import json
import json
d = dict(name='Bob', age=22, score=88)
d_jsoned = json.dumps(d)
print(d_jsoned)
print(type(d_jsoned))

json_str = '{"age": 20, "score": 88, "name": "Bob"}'
print(json.loads(json_str))


class Student(object):
    def __init__(self, name, age, score):
        self.name = name
        self.age = age
        self.score = score


def student2dict(stu):
    return {
        'name': stu.name,
        'age': stu.age,
        'score': stu.score
    }


s = Student('Bob', 20, 88)
print(json.dumps(s, default=student2dict))

def dict2student(d):
    return Student(d['name'], d['age'], d['score'])

json_str = '{"age": 20, "score": 88, "name": "Bob"}'
print(json.loads(json_str, object_hook= dict2student))
